package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.UserFriendMapper;
import com.ruoyi.system.domain.UserFriend;
import com.ruoyi.system.service.IUserFriendService;
import com.ruoyi.common.support.Convert;
import com.ruoyi.common.utils.StringUtils;

/**
 * 朋友关系 服务层实现
 * 
 * @author ruoyi
 * @date 2019-06-13
 */
@Service
public class UserFriendServiceImpl implements IUserFriendService 
{
	@Autowired
	private UserFriendMapper userFriendMapper;

	/**
     * 查询朋友关系信息
     * 
     * @param id 朋友关系ID
     * @return 朋友关系信息
     */
    @Override
	public UserFriend selectUserFriendById(Integer id)
	{
	    return userFriendMapper.selectUserFriendById(id);
	}
	
	/**
     * 查询朋友关系列表
     * 
     * @param userFriend 朋友关系信息
     * @return 朋友关系集合
     */
	@Override
	public List<UserFriend> selectUserFriendList(UserFriend userFriend)
	{
	    return userFriendMapper.selectUserFriendList(userFriend);
	}
	
    /**
     * 新增朋友关系
     * 
     * @param userFriend 朋友关系信息
     * @return 结果
     */
	@Override
	public int insertUserFriend(UserFriend userFriend)
	{
	    return userFriendMapper.insertUserFriend(userFriend);
	}
	
	/**
     * 修改朋友关系
     * 
     * @param userFriend 朋友关系信息
     * @return 结果
     */
	@Override
	public int updateUserFriend(UserFriend userFriend)
	{
	    return userFriendMapper.updateUserFriend(userFriend);
	}

	/**
     * 删除朋友关系对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteUserFriendByIds(String ids)
	{
		return userFriendMapper.deleteUserFriendByIds(Convert.toStrArray(ids));
	}
	
	

	/**
	 * 递归查询好友
	 * @param userFriendList 用户列表
	 * @param parentPhone 当前用户手机号
	 * @return
	 */
	public List<UserFriend> getUserFriendTree(List<UserFriend> userFriendList ,String parentPhone) {   
		UserFriend userFriend = new UserFriend();    
		userFriend.setParent(parentPhone);
		List<UserFriend> list = userFriendMapper.selectUserFriendList(userFriend);        
		if (StringUtils.isNotEmpty(list)) {
			for (UserFriend userFriend1 : list) {
				userFriendList.add(userFriend1);
				getUserFriendTree(userFriendList,userFriend1.getPHONE());
			}
		}
		return userFriendList;
		}
	
}
